// CriticalSection.h
// (c) 2008, Charles Lechasseur
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
// 
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

#ifndef RRO_UTILS_LOCKS_CRITICALSECTION_H
#define RRO_UTILS_LOCKS_CRITICALSECTION_H

#pragma once

#include <windows.h>


namespace RRO
{
    //
    // Simple CriticalSection wrapper to simplify creation and disposal.
    //
    class CriticalSection
    {
    public:
        //
        // Simple stack-based class to acquire a lock on a critical section.
        // The destructor releases the lock.
        //
        class Lock
        {
        public:
                                Lock(CriticalSection& p_rCriticalSection)
                                    : m_CriticalSection(p_rCriticalSection)
                                {
                                    // Acquire the lock immediately.
                                    m_CriticalSection.Enter();
                                }

                                ~Lock(void)
                                {
                                    // Release the lock when we're destroyed.
                                    m_CriticalSection.Leave();
                                }

        private:
            CriticalSection&    m_CriticalSection;

            // THESE METHODS ARE NOT IMPLEMENTED.
                                Lock(const Lock&);
            Lock&               operator=(const Lock&);
        };

                                CriticalSection(void);
                                ~CriticalSection(void);

                                //
                                // Enters the critical section. If the critical section is currently
                                // used by another thread, the calling thread is suspended until the
                                // critical section is released.
                                //
        void                    Enter(void)
                                {
                                    ::EnterCriticalSection(&m_CriticalSection);
                                }

                                //
                                // Tries to enter the critical section. If the critical section is currently
                                // used by another thread, the call returns false and doesn't acquire the
                                // critical section.
                                //
        bool                    TryEnter(void)
                                {
                                    return (::TryEnterCriticalSection(&m_CriticalSection) == TRUE);
                                }

                                //
                                // Releases the critical section. Each successful call to Enter() or TryEnter()
                                // must be balanced by a call to this method to make sure other threads can
                                // be resumed when they're waiting for the critical section.
        void                    Leave(void)
                                {
                                    ::LeaveCriticalSection(&m_CriticalSection);
                                }

    private:
        // Struct containing the Win32 critical section.
        CRITICAL_SECTION        m_CriticalSection;

        // THESE METHODS ARE NOT IMPLEMENTED.
                                CriticalSection(const CriticalSection&);
        CriticalSection&        operator=(const CriticalSection&);
    };

}; // namespace RRO


#endif RRO_UTILS_LOCKS_CRITICALSECTION_H
